package leecode

import (
	"fmt"
	"math"
)

func commonChars(words []string) (result []string) {
	hashMap := make(map[byte][]int) //key为每一个字符 , value为字符出现在第几个单词中
	for i := 0; i < len(words); i++ {
		for j := 0; j < len(words[i]); j++ {
			//如果是没有使用过的下标
			str := words[i]
			hashMap[str[j]] = append(hashMap[str[j]], i)
		}
	}
	fmt.Println(hashMap)
	for key, countArr := range hashMap {
		arr := countArr
		if len(countArr) < len(words) {
			continue
		}
		tempMap := make(map[int]int)
		minCount := math.MaxInt
		for i := 0; i < len(arr); i++ {
			tempMap[arr[i]]++
		}
		for _, count := range tempMap {
			minCount = min(minCount, count)
		}
		fmt.Println(tempMap)
		fmt.Println(string(key), minCount)
		if len(tempMap) == len(words) {
			for i := 0; i < minCount; i++ {
				result = append(result, string(key))
			}
		}
	}
	return
}
